﻿using System;
using System.Data.SqlClient;

namespace VegetableSale.NET.Utils
{
	public class SqlCommandExecutor
	{
        private DBConnection _dbConnection;
        private SqlConnection _connection;
        private SqlCommand _sqlCommand;

        public SqlCommandExecutor()
        {
            _dbConnection = new DBConnection();
        }

        public SqlDataReader Execute(string sql)
        {
            _connection = _dbConnection.GetConnection();
            _sqlCommand = new SqlCommand(sql, _connection);
            SqlDataReader sqlDataReader = _sqlCommand.ExecuteReader();

            return sqlDataReader;
        }

        public int ExecuteCount(string sql)
        {
            _dbConnection.GetConnection();

            _connection = _dbConnection.GetConnection();
            _sqlCommand = new SqlCommand(sql, _connection);

            int i = _sqlCommand.ExecuteNonQuery();

            return i;
        }

        public SqlCommand PrepareSqlCommand(string sql)
        {
            _connection = _dbConnection.GetConnection();
            _sqlCommand = new SqlCommand(sql, _connection);
            return _sqlCommand;
        }

        public void CloseConnection()
        {
            if (_connection != null)
            {
                _dbConnection.Close(_connection);
            }
        }
    }
}

